package com.jjyu.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.jjyu.entity.Inventory;
import com.jjyu.entity.Restaurant;
import com.jjyu.service.InventoryService;
import com.jjyu.service.LLMService;
import com.jjyu.service.RestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class LLMServiceImpl implements LLMService {
    @Autowired
    private InventoryService inventoryService;
    @Autowired
    private RestaurantService restaurantService;

    @Override
    public String getSurprise() {
        StringBuilder msg = new StringBuilder("我想吃什么，但是不知道吃什么，你能从下面的店铺和他对应的菜品种，帮我选一个组合吗？一定要给个组合，不要问我问题 \n 饭店与菜品：");
        System.out.println("msg:" + msg);
        List<Restaurant> restaurantList = restaurantService.list();
        List<Inventory> inventoryList = inventoryService.list();
        for (Restaurant restaurant : restaurantList) {
            msg.append("\n").append(restaurant.getName()).append("：");
            for (Inventory inventory : inventoryList) {
                if (restaurant.getRestaurantId().equals(inventory.getRestaurantId())) {
                    msg.append(inventory.getItemName()).append(" ");
                }
            }
        }
        try {
            System.out.println("msg:" + msg);
            GenerationResult result = callWithMessage(msg.toString());
            System.out.println(result.getOutput().getChoices().get(0).getMessage().getContent());
            return result.getOutput().getChoices().get(0).getMessage().getContent();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.err.println("错误信息：" + e.getMessage());
            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
            return "err：" + e.getMessage();
        }
    }

    public static GenerationResult callWithMessage(String msg) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        Message systemMsg = Message.builder()
                .role(Role.SYSTEM.getValue())
                .content("你是一位美食家")
                .build();
        Message userMsg = Message.builder()
                .role(Role.USER.getValue())
                .content(msg)
                .build();
        GenerationParam param = GenerationParam.builder()
                // 若没有配置环境变量，请用百炼API Key将下行替换为：.apiKey("sk-xxx")
                .apiKey("")
                // 模型列表：https://help.aliyun.com/zh/model-studio/getting-started/models
                .model("qwq-32b-preview")
                .messages(Arrays.asList(systemMsg, userMsg))
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .build();
        return gen.call(param);
    }
}
